<template>
    <div class="layout-container">
        <div class="layout-container-left">
            <UseTable :tableData="tableData" unique @change="change" />
        </div>

        <div class="layout-container-right">
            <div class="form-box">
                <el-form label-width="auto">
                    <el-form-item label="平均分布">
                        <el-input v-model="evenDistribution" type="number" @change="evenDistributionChange" />
                    </el-form-item>

                    <el-form-item label="区间划分值">
                        <el-row>
                            <el-col v-for="(input, index) in paramsData.bins" :key="index" class="input-row" :span="8">
                                <el-input v-model="paramsData.bins[index]" :disabled="index === 0||index===paramsData.bins.length-1" clearable @clear="clearHandler(index)" />
                                <el-button v-if="index!==paramsData.bins.length-1" @click="addArea(index)">-</el-button>
                            </el-col>
                        </el-row>
                    </el-form-item>

                    <el-form-item>
                        <div class="data-select">
                            <DmData @dataChange="dataChange" />
                            <Upload @uploadSuccess="uploadSuccess" />
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="handleCalculate">计算</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <el-divider />

            <div class="info-box">
                <h5>算法说明</h5>
                <p>计算数据的频率分布、偏度（对称性：>0 右偏，<0 左偏）和峰度（陡峭度：>0 尖峰，<0 平峰），描述数据分布特征。</p>

                <h5>使用范围</h5>
                <ul>
                    <li>了解数据的整体形态（如考试成绩是否呈正态分布）；</li>
                    <li>为后续分析选择合适的统计方法（如偏态数据需非参数检验）。</li>
                </ul>
            </div>
        </div>

        <Result v-model="resultShow" :originalData="paramsData" :result="resultData" />
    </div>
</template>

<script setup lang="ts">
import {onActivated, onMounted, ref} from "vue"
import {ElMessage}                   from "element-plus"
import {distribution}                from "@/api/descriptive"
import Result                        from "./components/Result.vue"
import {getExcel}                    from "@/api/data"
import {isMonotonicIncreasing}       from "@/utils/arrayUtils"
import Upload                        from "@/components/Upload.vue"
import UseTable                      from "@/components/UseTable.vue"
import DmData                        from "@/components/DmData.vue"

function dataChange(data) {
    tableData.value = data
}

function uploadSuccess(res) {
    tableData.value = res.data
}

let evenDistribution = ref(8)

function evenDistributionChange() {
    let min = Math.min(...paramsData.value.data) | 0
    let max = Math.max(...paramsData.value.data) | 0

    // 计算均匀分布的bins
    const bins = [min]
    const step = (max - min) / evenDistribution.value

    // 插入中间值
    for (let i = 1; i < evenDistribution.value; i++) {
        const value = min + step * i
        // 保留两位小数
        bins.push(Number(value.toFixed(2)))
    }

    // 添加最大值
    bins.push(max)

    paramsData.value.bins = bins
}

function change(list) {
    paramsData.value.data = list?.[0]?.value || []

    evenDistributionChange()
}

// 处理输入变化
let addArea = (index) => {
    paramsData.value.bins.splice(index + 1, 0, "")
}

function clearHandler(index) {
    paramsData.value.bins.splice(index, 1)
}

let paramsData = ref({
    data: [],
    bins: [0, 0]
})

let resultShow = ref(false)
let resultData = ref({})

// 补充参数
function handleCalculate() {
    if (paramsData.value.data.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }

    let isMonotonic = isMonotonicIncreasing(paramsData.value.bins)
    if (!isMonotonic) {
        ElMessage.warning("区间应该单调递增")
        return
    }

    distribution(paramsData.value).then((res: any) => {
        resultShow.value = true
        resultData.value = res?.data
    })
}

let loading   = ref(true)
let tableData = ref([])

let getTableData = () => {
    loading.value = true

    getExcel("distribution").then(res => res.data).then(data => tableData.value = data)

    loading.value = false
}

/**
 * 初始化参数
 */
function initParam() {

}

// -----------------------------主程序start-----------------------------------------
onMounted(() => {
    initParam()
    getTableData()
})

onActivated(() => {
    initParam()
    getTableData()
})
// -----------------------------主程序end-----------------------------------------

</script>

<style scoped>
.input-row {
    display: flex;
    align-items: center;
}
</style>

